package com.jintoufs.service.travelagency.impl;

import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.travelagency.TravelAgencyMapper;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.shop.Shop;
import com.jintoufs.domain.shop.ShopManager;
import com.jintoufs.domain.travelagency.TravelAgency;
import com.jintoufs.service.shop.ShopManagerService;
import com.jintoufs.service.shop.ShopService;
import com.jintoufs.service.travelagency.TravelAgencyService;
import com.jintoufs.uuid.SnowflakeIdWorker;
import org.apache.commons.lang.StringUtils;
import org.cosmos.modules.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : zhenghq
 * @Despription :
 * @Date 2018/1/25 15:13
 * @Modified by
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TravelAgencyServiceImpl implements TravelAgencyService {

    @Autowired
    TravelAgencyMapper travelAgencyMapper;

    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;

    @Autowired
    ShopService shopService;

    @Autowired
    ShopManagerService shopManagerService;

    @Override
    public void save(TravelAgency travelAgency, Customer customer) throws ServiceException {
        travelAgency.setStatus("0");//待审核
        if(StringUtils.isEmpty(travelAgency.getId())){
            travelAgency.setId(getUUID());
            travelAgency.setManagerId(customer.getId());
            travelAgency.setManagerName(customer.getRealName());
            travelAgency.setCreateTime(new Date());
            travelAgencyMapper.insertSelective(travelAgency);
            Shop shop = new Shop();
            shop.setName(travelAgency.getName());
            shop.setValueId(travelAgency.getId());
            Shop now = shopService.save(shop,getUUID());
            ShopManager shopManager = new ShopManager();
            shopManager.setShopId(now.getId());
            shopManager.setCustomerId(customer.getId());
            shopManagerService.save(shopManager,getUUID());
        }else {
            String curStatus = travelAgencyMapper.selectByPrimaryKey(travelAgency.getId()).getStatus();
//            if(!"0".equals(curStatus) && !"2".equals(curStatus)){
//                throw new ServiceException("该假期当前状态不允许编辑，请检查！");
//            }
            travelAgency.setLastModifyBy(customer.getId());
            travelAgency.setLastModifyByName(customer.getRealName());
            travelAgency.setLastModifyTime(new Date());
            travelAgencyMapper.updateByPrimaryKeySelective(travelAgency);
        }

        if(StringUtils.isNotEmpty(travelAgency.getManagerId())){
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("travel_agency_id",travelAgency.getId());
            List<Map<String,Object>> info = queryTravelAgencyInfo(params);
            if(info!=null && info.size()>0){
                String managerId = (String)info.get(0).get("customer_id");
                if(StringUtils.isNotEmpty(managerId)){
                    params.put("valueId",travelAgency.getId());
                    Shop shop = shopService.selectByParam(params).get(0);
                    ShopManager shopManager = new ShopManager();
                    shopManager.setCustomerId(travelAgency.getManagerId());
                    shopManager.setShopId(shop.getId());
                    shopManagerService.save(shopManager,null);
                }
            }else{
                Shop shop = new Shop();
                shop.setValueId(travelAgency.getId());
                String shopId= getUUID();
                shopService.save(shop,shopId);
                ShopManager shopManager = new ShopManager();
                shopManager.setCustomerId(travelAgency.getManagerId());
                shopManager.setShopId(shopId);
                shopManagerService.save(shopManager,getUUID());
            }
        }


    }

    @Override
    public void deleteTravelAgencyById(String id) throws ServiceException {
        TravelAgency travelAgency = getTravelAgencyById(id);
        if(travelAgency==null){
            throw new ServiceException("未找到对应的假期，请检查！");
        }
        travelAgency.setStatus("0");
        travelAgencyMapper.updateByPrimaryKeySelective(travelAgency);
//        travelAgencyMapper.deleteByPrimaryKey(id);
    }

    @Override
    public TravelAgency getTravelAgencyById(String id){
        return travelAgencyMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Map> getByFilter(QueryFilters filters) {
        return travelAgencyMapper.getByFilter(filters);
    }

    @Override
    public List<Map> getCustomerInfoByFilter(QueryFilters filters) {
        return travelAgencyMapper.getCustomerInfoByFilter(filters);
    }

    @Override
    public List<Map<String,Object>> queryCustomerInfo(Map<String,Object> map) {
        return travelAgencyMapper.queryCustomerInfo(map);
    }

    @Override
    public List<Map<String,Object>> queryTravelAgencyInfo(Map<String, Object> map) {
        return travelAgencyMapper.queryTravelAgencyInfo(map);
    }

    @Override
    public void auth(String agencyId, String status) throws ServiceException {
        TravelAgency travelAgency = travelAgencyMapper.selectByPrimaryKey(agencyId);
        if(travelAgency==null){
            throw new ServiceException("未找到对应的假期信息，请检查！");
        }
        if("1".equals(travelAgency.getStatus())){
            throw new ServiceException("该旅行已审核通过，不能再次审核");
        }
        travelAgency.setStatus(status);
        travelAgencyMapper.updateByPrimaryKeySelective(travelAgency);
    }

    private String getUUID(){
        return String.valueOf(snowflakeIdWorker.nextId());
    }
}
